<?php   
include_once("../helper/DBOperation.php");
$dbObj = new DBOperation();	    

include("../helper/check_user_login.php");
checkUserIsLogin("admin");

include_once("includes/function.php");
	  
if($_POST){
	$username  = $_POST['username'];
	$password  = $_POST['password'];
	$cpassword = $_POST['cpassword'];
	$email     = $_POST['email'];
	$phone     = $_POST['phone'];
	$carrier     = $_POST['carrier'];
	$display_name = $_POST['display_name'];
	$error ="";
	$success ="";

	if($username==""){
		$error.= "Username can not be left blank. <BR>";
	}
	
	

	if(strstr($username," ")){
		$error.= "Username can not contain space. <BR>";
	}
	
	if($password==""){
		$error.= "Password can not be left blank. <BR>";
	}

	if($cpassword=="" || $cpassword!=$password){
		$error.= "Password and Confirm Password does not match. <BR>";
	}

	if($email==""){
		$error.= "Email can not be left blank . <BR>";
	}elseif(is_valid_email($email) == FALSE){
		$error.= "Please Enter Correct Email . <BR>";
	}

	if($display_name==""){
		$error.= "Display Name can not be left blank. <BR>";
	}

	$username  = mysql_real_escape_string($username);
	$chk_uname = mysql_num_rows(mysql_query("select * from tbl_users where username = '".$username."'"));
	if($chk_uname>0){
		$error.= "This Username already exist. <BR>";
	}

	if($error==""){		
		
		$password = mysql_real_escape_string($password);
		$email    = mysql_real_escape_string($email);
		$phone    = mysql_real_escape_string($phone);
		$carrier    = mysql_real_escape_string($carrier);
		$display_name = mysql_real_escape_string($display_name);

		$query = "Insert into tbl_users set username='".$username."', password = '".md5($password)."', email = '".$email."', phone = '".$phone."', carrier = '".$carrier."', display_name='".$display_name."'";

		if(mysql_query($query)){
			$username = ""; $email = ""; $display_name=""; $phone=""; $carrier="";
			$_SESSION['success'] = "User has been added successfully.";			
			header("location:user_listing.php");
		}else{
			$error = "User can not be added this time. Please try again later.";
		}		
	}
	
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Welcome to Admin Panel</title>
	<link href="css/style.css" rel="stylesheet" type="text/css" />
	<script language="JavaScript" src="js/jquery.js"></script>
	<script language="JavaScript" src="js/jquery.validate.js"></script>
</head>
<body>
<div id="container">
	<div id="navigation">
		<div id="title">Add User</div>
		<div id="nav"><a href="../unit_a.php">Go To Mobile Site</a> | <a href="user_listing.php">User Listing</a> | <a href="logout.php">Logout</a></div>
	</div>
	<div id="body">
		<div id="user_listing">
			<div id="error"><?php if(isset($error) && $error!="") { echo $error; }?></div>
			<form name="webform" id="webform" action="add_user.php" method="post">
				<table id="add_user">
					<tr>
						<td id="lbl">Username <span id="required"> * </span>: </td>
						<td><input type='text' id="username" name="username" value="<?php if(isset($username))  echo $username;?>" class="text_field_bg required" /></td>
					</tr>
					<tr>
						<td>Password <span id="required"> * </span>: </td>
						<td><input type='password' id="password" name="password" value="" class="text_field_bg password"/></td>
					</tr>
					<tr>
						<td>Confirm Password <span id="required"> * </span>: </td>
						<td><input type='password' id="cpassword" name="cpassword" value="" /></td>
					</tr>
					<tr>
						<td>Email <span id="required"> * </span>: </td>
						<td><input type='text' id="email" name="email" value="<?php if(isset($email))  echo $email;?>" class="required email" /></td>
					</tr>
                    <tr>
						<td>Phone : </td>
						<td><input type='text' id="phone" name="phone" value="<?php if(isset($phone))  echo $phone;?>"  /></td>
					</tr>
                    <tr>
						<td>Carrier : </td>
						<td>
                        <select id="carrier" name="carrier">
    <option  value="">--Select--</option>
    <option <?php if ($carrier=="txt.att.net") { ?> selected="selected"<?php } ?> value="txt.att.net">ATT</option>
    <option <?php if ($carrier=="messaging.sprintpcs.com") { ?> selected="selected"<?php } ?> value="messaging.sprintpcs.com">SPRINT</option>
    <option <?php if ($carrier=="tmomail.net") { ?> selected="selected"<?php } ?> value="tmomail.net">TMOBILE</option>
    <option <?php if ($carrier=="email.uscc.net") { ?> selected="selected"<?php } ?> value="email.uscc.net">US_CELLULAR</option>
    <option <?php if ($carrier=="vtext.com") { ?> selected="selected"<?php } ?> value="vtext.com">VERIZON</option>
    <option <?php if ($carrier=="vmobl.com") { ?> selected="selected"<?php } ?> value="vmobl.com">VIRGIN MOBILE</option>
</select>
                    
                        </td>
					</tr>
					<tr>
						<td>Display Name <span id="required"> * </span>: </td>
						<td><input type='text' id="display_name" name="display_name" value="<?php if(isset($display_name))  echo $display_name;?>" class="text_field_bg required"/></td>
					</tr>
					<tr>
						<td id="lbl"></td>
						<td><input type='submit' id="submit" name="submit" value="Submit" /></td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</div>
</body>

<script>
	$(document).ready(function(){	
		jQuery.validator.addMethod("noSpace", function(value, element) { 
  return value.indexOf(" ") < 0 && value != ""; 
}, "No space please allowed.");	

		$("#webform").validate({
			  rules: {
				'password': "required",
				'cpassword': {
				  equalTo: "#password"
				 },
      				'username': {
          			   noSpace: true
      				}
			  }
		});
	});

</script>
</html>
